package com.springboot.mybatis.service.rabbitmq;

import com.rabbitmq.client.*;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author sunyt31481
 * @date 2020/8/11 16:01
 * @description
 */
public class CustomerDemoTest {
    private static Object IOException;
    private static Object TimeoutException;

    public static void main(String[] args) throws IOException, TimeoutException {
        //使用@test方式会在运行一次之后就停止，无法持续监听消息信息，所以使用main（）方法形式

        /*//创建连接对象
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //连接主机
        connectionFactory.setHost("localhost");
        //设置端口号
        connectionFactory.setPort(5672);
        //连接虚拟主机
        connectionFactory.setVirtualHost("/ems");
        connectionFactory.setUsername("ems");
        connectionFactory.setPassword("1");

        //连接通道
        Connection connectionn = connectionFactory.newConnection();*/

        Connection connection = RabbitMqUtil.getConnect();
        Channel channel = connection.createChannel();

        //绑定队列信息
        channel.queueDeclare("helloQueue",false,false,false,null);
        //消费信息
        //参数1：队列名字
        //参数2：自动确认机制
        //参数3：消费时的回调函数，拿到消息信息做处理
        channel.basicConsume("helloQueue",true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body));
            }
        });
}
}
